@echo off
cls
cd %ROOT_DIR%
if not exist "config.bat" (
	echo ERROR: config.bat not found
	goto END
)
call config.bat

:CHECKING
echo ::Checking
if not exist %JDK% (
    echo ERROR: JDK [%JDK%] not found
	goto END
)
if not exist %DATA_DIR% (
    echo ERROR: JDK [%DATA_DIR%] not found
	goto END
)
if not exist %SOURCE_DIR% (
    echo ERROR: JDK [%DATA_DIR%] not found
	goto END
)
if not exist %TOOLPACK% (
    echo ERROR: JDK [%TOOLPACK%] not found
	goto END
)

:BEGIN
cd %DATA_DIR%
if exist #build (rd /s /q #build)
if exist #temp (rd /s /q #temp)

:CLONE_RES
echo ========CLONE RESOURCE========
dir /B/S *.*  > #raw_files.txt
md #temp
md #build
for /F %%i in (#raw_files.txt) do (
    copy %%i #temp>nul
)
copy %TOOLPACK% #temp>nul
cd .\#temp
SET TOOLPACK_JAR=%TOOLPACK%
for /F "tokens=2 delims==" %%i  in ('set TOOLPACK_JAR') do (
    set TOOLPACK_JAR=%%~nxi
)

:CONVERT_IMAGE
echo ========CONVERT IMAGE TO TGA========
rem dir /B/S *.jpg, *.png, *.bmp  > #raw_files.txt
rem for /F %%i in (#raw_files.txt) do (
rem     %JDK%\java -jar -Xmx512M %TOOLPACK_JAR% tga %%i %%~ni.tga
rem )
:BUILD_TEXT
echo ========BUILD TEXT========
%JDK%\java -jar -Xmx512M %TOOLPACK_JAR% le text.xls
copy /y .\*.ff ..\#build>nul
copy /y .\*.lang ..\#build>nul
copy /y .\*.h %SOURCE_DIR%>nul

:COPY_RES
echo ========COPY RESOURCE========
dir /B/S *.wav, *.ogg, *.spr, *.png, *.lel  > #raw_files.txt
for /F %%i in (#raw_files.txt) do (
    copy /y %%i ..\#build\%%~nxi>nul
)

:END
echo ========DONE========
cd %CUR_DIR%
if exist %DATA_DIR%\#temp (rd /s /q %DATA_DIR%\#temp)
if exist %DATA_DIR%\#raw_files.txt (del %DATA_DIR%\#raw_files.txt>nul)
